<?php

require_once './include/common.inc.php';
checkLogin();
$page_title = 'jans2en furniture - Search - Search';

getMyParam('searchtext');
getMyParam('viewall');
$searchtext = trim($searchtext);
$product_ary = array();

// ûʾwholesale customerֻܿhongkong warehouse
$sql_where = '';
//if ($jansen_type == 1) {
//    $inventory_ary = array();
//    $sql = "SELECT `id` FROM `inventory` WHERE `type` = 1 ";
//    $query = $db->query($sql);
//    while($rs = $db->fetch_array($query)) {
//        $inventory_ary[] = $rs['id'];
//    }
//
//    if (!empty($inventory_ary)) {
//        $inventory_str = implode(",", $inventory_ary);
//        $sql_where = " AND i.`inventory_id` IN (" . $inventory_str . ")";
//    } else {
//        $sql_where = " AND i.`inventory_id` = 0 ";
//    }
//} else {
//    $sql_where = " AND i.`inventory_id` > 0 ";
//}

$inventory_ary = array();
$sql = "SELECT `id` FROM `inventory`";
$query = $db->query($sql);
while($rs = $db->fetch_array($query)) {
	$inventory_ary[] = $rs['id'];
}
$inventory_str = implode(",", $inventory_ary);
if ($jansen_type == 1) {
    if (!empty($inventory_ary)) {
        $sql_where = " AND i.`inventory_id` IN (" . $inventory_str . ")";
    } else {
        $sql_where = " AND i.`inventory_id` = 0 ";
    }
} else {
    $sql_where = " AND i.`inventory_id`  IN (" . $inventory_str . ") ";
}


// *** page code start ***
$page_url = 'search_result.php?searchtext=' . urlencode($searchtext) . '&';

//$sql = "SELECT `id` FROM `product` WHERE `name` LIKE '%$searchtext%' OR `code` LIKE '%$searchtext%' AND `hidden_flag`=0";
$sql = "SELECT p.`id` FROM `product` p, `product_inventory` i WHERE p.`id`=i.`product_id` $sql_where AND (p.`name` LIKE '%$searchtext%' OR p.`code` LIKE '%$searchtext%') AND p.`hidden_flag`=0 AND i.qty>0";

$query = $db->query($sql);
$sql_all_num1 = $db->num_rows($query);

$sql = "SELECT p.`id`, p.`code`, p.`name`, p.`pic`, p.`small_pic`, p.`price_1`, p.`price_2`, p.`price_3`, p.`new_flag`, c.`category_id` FROM `product` p, `product_category` c , `category` pc WHERE not exists(select id from category where id=pc.fid and publish = 0) and p.`id`=c.`product_id` AND c.category_id=pc.id AND pc.publish=1 AND (p.`name` LIKE '%$searchtext%' OR p.`code` LIKE '%$searchtext%') AND p.`hidden_flag`=0  GROUP BY p.`id`";

$query = $db->query($sql);
$sql_all_num2 = $db->num_rows($query);

$sql_all_num = $sql_all_num1 + $sql_all_num2;

if ($sql_all_num > 0 ) {
    // ¼ûʵҳϢ
    browseTracking(6,0,$searchtext);
}

$ppp = 12;
getMyParam('page');
$page = empty($page) || !ispage($page) ? 1 : $page;
$start_limit = ($page - 1) * $ppp;
if($start_limit > $sql_all_num) {
    $start_limit = 0;
    $page = 1;
}

if ($start_limit+$ppp > $sql_all_num1) {
    //$start_limit2 = $start_limit+$ppp - $sql_all_num1;
    if($start_limit < $sql_all_num1) {
        $start_limit2 = 0;
        $ppp2 = $start_limit+$ppp-$sql_all_num1;
    } else {
        $start_limit2 = $start_limit-$sql_all_num1;
        $ppp2 = $ppp;
    }
}

/*
echo "start_limit:".$start_limit;echo "<br>";  //0
echo "sql_all_num:".$sql_all_num;  //8
echo "<br>";
echo $sql_all_num1;echo "<br>";  //2
echo $sql_all_num2;echo "<br>";  //6
echo $start_limit2;echo "<hr>";
echo $ppp2;echo "<br>";
exit;
*/

$page_show = getPhotoPageStr($sql_all_num, $page, $ppp, $page_url, 'items', ' for search text <strong>\'' . $searchtext . '\'</strong>');
// *** page code end ***

$photo_ary = array();

$sql_limit = '';
if ($start_limit<$sql_all_num1 || $start_limit == $sql_all_num1) {
    // inventoryеĽ
    // ҪǵûѡĲƷ
	
	if(!isset($viewall))
		$sql_limit = " LIMIT ".$start_limit.", ".$ppp;
	
    //$sql = "SELECT `id`, `code`, `name`, `pic`, `small_pic`, `price_1`, `price_2`, `price_3`, `new_flag` FROM `product` WHERE `name` LIKE '%$searchtext%' OR `code` LIKE '%$searchtext%' AND `hidden_flag`=0 ORDER BY `id` DESC LIMIT $start_limit, $ppp";
	//$sql = "SELECT p.`id`, p.`code`, p.`name`, p.`pic`, p.`small_pic`, p.`price_1`, p.`price_2`, p.`price_3`, p.`new_flag`, p.`finish_id`, i.`inventory_id` FROM `product` p, `product_inventory` i, `product_category` c WHERE p.id=c.`product_id` AND p.`id`=i.`product_id` $sql_where AND (p.`name` LIKE '%$searchtext%' OR p.`code` LIKE '%$searchtext%') AND p.`hidden_flag`=0 AND i.qty>0 GROUP BY p.`id` ORDER BY p.`code` DESC".$sql_limit;
	$sql = "SELECT p.`id`, p.`code`, p.`name`, p.`pic`,p.fabricshown,p.leathershown, p.`small_pic`, p.`price_1`, p.`price_2`, p.`price_3`, p.`new_flag`, p.`finish_id`, i.`inventory_id` FROM `product` p, `product_inventory` i WHERE p.`id`=i.`product_id` $sql_where AND (p.`name` LIKE '%$searchtext%' OR p.`code` LIKE '%$searchtext%') AND p.`hidden_flag`=0 AND i.qty>0 GROUP BY p.`id` ORDER BY p.`code` DESC".$sql_limit;
  
 $query = $db->query($sql);
    while($rs = $db->fetch_array($query)) {
        //$product_ary[] = array('id'=>$rs['id'], 'name'=>$rs['name'], 'pic'=>$rs['pic'], 'code'=>$rs['code']);
		$rs['search_type'] = 'inventory';
		$rs['code'] =strtoupper($rs['code']);
        $product_ary[] = $rs;
        
        $js_id_ary[] = "'" . $rs['id'] . "'";
        $sp_id_ary[] = $rs['id'];
    }
}

if ($start_limit+$ppp > $sql_all_num1) {
	
	if(!isset($viewall))
		$sql_limit = "  LIMIT ".$start_limit2.", ".$ppp2;
	
    // productеĽ
	$sql = "SELECT p.`id`, p.`code`, p.`name`, p.`pic`, p.`small_pic`, p.`price_1`, p.`price_2`, p.`price_3`, p.`new_flag`, p.`finish_id`, c.`category_id` FROM `product` p, `product_category` c , `category` pc WHERE not exists(select id from category where id=pc.fid and publish = 0) and p.`id`=c.`product_id` AND c.category_id=pc.id AND pc.publish=1 AND p.`id`=c.`product_id` AND c.category_id=pc.id AND pc.publish=1 AND (p.`name` LIKE '%$searchtext%' OR p.`code` LIKE '%$searchtext%') AND `hidden_flag`=0 GROUP BY p.`id` ORDER BY p.`code` DESC ".$sql_limit;
    $query = $db->query($sql);
    while($rs = $db->fetch_array($query)) {
		$rs['search_type'] = 'product';
		$rs['code'] =strtoupper($rs['code']);
        $product_ary[] = $rs;
        //$js_id_ary[] = "'" . $rs['id'] . "'";
        $sp_id_ary[] = $rs['id'];
    }
}







if (!empty($sp_id_ary)) {
    $sp_id_str = implode(",", $sp_id_ary);
    $sql = "SELECT `inventory_id`, `product_id`, `qty`, `special_price`, `special_price2` FROM `product_inventory` WHERE `product_id` IN (" . $sp_id_str . ")";
    $query = $db->query($sql);
    while($rs = $db->fetch_array($query)) {
        $sp_ary[$rs['product_id']][$rs['inventory_id']] = $rs['special_price'];
        $sp_ary2[$rs['product_id']][$rs['inventory_id']] = $rs['special_price2'];
        $product_qty_ary[$rs['product_id']][$rs['inventory_id']] = $rs['qty'];
    }

    $sql = "SELECT p.`product_id`, p.`category_id`, c.`fid` FROM `product_category` p, `category` c WHERE p.`category_id`=c.`id` AND `product_id` IN (" . $sp_id_str . ")";
    $query = $db->query($sql);
    while($rs = $db->fetch_array($query)) {
        if ($rs['fid'] == 0) {
            $product_category_ary[$rs['product_id']] = $rs['category_id'];
            $product_category_ary2[$rs['product_id']] = $rs['category_id'];
        } else {
            $product_category_ary[$rs['product_id']] = $rs['fid'];
            $product_category_ary2[$rs['product_id']] = $rs['category_id'];
        }
    }

}


$toUrl = 'product_photo_list.php?categoryid=%d';
if ($jansen_type > 0 && $jansen_uid > 0) 
{	
	$toUrl = 'product_list.php?categoryid=%d';
}
$vNull = false;
$category_list = GetCategoryTable($_categories,$toUrl,0,$categoryid,&$vNull);
$postiondiv='<div class="position" id="position"><a href="index.php">Home</a> &gt; <a href="products.php?categoryid=1001"> Product</a> </div>';
$baner_img='<div class="bigtitle">Product</div>';


$js_id_ary_str = !empty($js_id_ary) ? "new Array(" . implode(',', $js_id_ary) . ")" : "new Array()";

$PagerString = '';
if(isset($viewall) && $viewall==1)
{
	$PagerString = 'Viewing all '.$sql_all_num.' Records.';
	
	if($searchtext!="")  //add by martin 2010 -12-27
	{
	   $page_show['str'] = 'You are now viewing all items for search text <strong>\'' . $searchtext . '\'</strong>'; 	
	}
	else
	{
	    $page_show['str']=""; 
	}
}
else
{
	$PagerString = 'Page  '.$page_show['pagelink'].'<a target="_self" href="search_result.php?searchtext=' . urlencode($searchtext) . '&viewall=1">View all</a>';
}


//fabrics
		//$fabrics_cat_ary=array();
		$fabrics_ary='var fabrics_cat_ary=[{';
		$sql = "SELECT * FROM `p_fabric` ORDER BY `seq`";
		$query = $db->query($sql);
		
		while($rsf = $db->fetch_array($query)) 
		{		$fabrics_ary.='"'.strtolower($rsf['name']).'":"'.$rsf['id'].'",';
		 
		}
		if($fabrics_ary=="fabrics_ary={")
		$fabrics_ary="";
		else
		$fabrics_ary.="'jantests':'1000'}]";
		
		
        $sql = "SELECT * FROM `p_leather`";	
			$leathers_ary='leathers_ary=[{';	
		$query = $db->query($sql);		
		while($rsl = $db->fetch_array($query))	
		{
		  //$leathers_item_ary[$rsl['cat_id']][]=array($rs['id']=>$rsl['name']);	
		  $leathers_ary	 .='"'.$rsl['name'].'":"'.$rsl['id'].'",';
		}
		if( $leathers_ary=='leathers_ary=[{')
		$leathers_ary='';
		else
		$leathers_ary.='"cssss":"10000"}]';



require_once PrintEot('header3');
if (empty($product_ary)) {
	// -------------- guideͼƬ start ----------------------------------------
	$img = getSystemValue("picture_other_searchen");
	
	if (trim($img) == "" || !file_exists("./zh/attachments/system/" . $img)) {
		$img = './zh/attachments/no.gif';
	} 
	else
	{
		$img = './zh/attachments/system/'.$img;	
	}
	// -------------- guideͼƬ end ----------------------------------------
	
    require_once PrintEot('search_result2');
} else {
    require_once PrintEot('search_result');
}
require_once PrintEot('footer');

?>
